import {action, observable, runInAction} from "mobx";
import {BaseResInterface, BaseResInterfaceWithData, sendGetRequest, sendPostRequest} from "../../../net";
import {OrderModelInterface, PageInterface} from "../../../net/instance/CommenModelInstance";
import {defaultArr, defaultObj} from "../../../util";
import {productOrderItem} from "../../../page/primary/checkBill";
import UserStoreInstance from "../user";
import AddressStoreInstance from "../user/address";

interface OrderResInterface extends BaseResInterface {
    data: OrderStoreInterface
}

export interface OrderStoreInterface {
    rows: OrderModelInterface[],
    count: number
}

interface orderParams {
    userId: number,
    recommenderId: number,
    isMention: number,
    shipmentId: number,
    canDeductionScore: number,
    products: productOrderItem[],
    totalPrice: number
}

interface payInfoInstance {
    timeStamp: string,
    nonce_str: string,
    mch_id: string,
    appid: string,
    prepay_id: string,
    sign: string
}

type  PayPlatform = "alipay" | "wechat";

export class OrderStore {

    @observable selectedOrder: OrderModelInterface = null;


    async getOrderList(page: PageInterface, userId: number, status: number) {
        const {data} = await sendPostRequest<OrderResInterface>("order/getOrderListByState", {
            page,
            userId,
            status
        });
        return data;
    }

    /***
     * 修改订单状态
     * @param {number} status
     * @param {number} orderId
     * @returns {Promise<void>}
     */
    async changeOrderStatus(status: number, orderId: number) {
        const {msg} = await sendPostRequest<BaseResInterface>("order/updateOrderStatus", {
            orderId,
            status
        });
        return msg;
    }

    /***
     * 取消订单
     * @param orderId
     */
    async deleteOrder(orderId: number) {
        const {msg} = await sendPostRequest<BaseResInterface>("order/deleteOrder", {
            orderId
        });
        return msg;
    }

    /***
     * 支付已创建订单
     */

    async payOrder(platform: PayPlatform) {
        const {data} = await sendPostRequest<BaseResInterfaceWithData<payInfoInstance>>("order/payCreatedOrder", {
            orderId: this.selectedOrder.id,
            userId: UserStoreInstance.user.id,
            platform
        });
        return data;
    }

    /***
     * 创建订单
     */
    @action.bound
    async createOrder(products: productOrderItem[], isMention: number, totalPrice: number, canDeductionScore: number) {
        const user = UserStoreInstance.user;
        const params: orderParams = {
            userId: user.id,
            recommenderId: user.recommenderId,
            isMention,
            products,
            totalPrice,
            canDeductionScore,
            shipmentId: AddressStoreInstance.userAddress.id
        };
        const {msg, data} = await sendPostRequest<BaseResInterfaceWithData<OrderModelInterface>>("order/createOrder", params);
        runInAction(() => {
            this.setSelectedOrderId(data);
        });
        return msg;
    }

    /***
     * 设置需要支付的订单
     */
    @action.bound
    setSelectedOrderId(order: OrderModelInterface) {
        this.selectedOrder = order;
    }

    /***
     * 免费的支付回调
     */
    @action.bound
    async freePayNotify() {
        const {msg} = await sendPostRequest<BaseResInterface>("order/freePayNotify", {
            orderId: this.selectedOrder.id,
        });
        return msg;
    }
}

const OrderStoreInstance = new OrderStore();
export default OrderStoreInstance